package com.example.shop_2211.search

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.TextView
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.base.mvvm.BaseActivity
import com.example.shop_2211.R
import com.example.shop_2211.adapter.GoodsAdapter
import com.example.shop_2211.databinding.ActivitySearchNewBinding
import com.example.shop_2211.roomnew.Record
import com.example.shop_2211.roomnew.RecordDBUtils
import com.google.android.material.chip.Chip

@Route(path = "/app/SearchNewActivity")
class SearchNewActivity : BaseActivity<ActivitySearchNewBinding,SearchNewViewModel>() {
    lateinit var goodsAdapter: GoodsAdapter
    val list1 = mutableListOf<String>()
    override fun initView() {
        list1.add("http://10.161.9.80:8080/crmebimage/public/maintain/2021/12/25/a272fcefed804cf2b1e6785d5662c29ejqjxha0eow.jpg")
        list1.add("http://10.161.9.80:8080/crmebimage/public/maintain/2021/12/25/a272fcefed804cf2b1e6785d5662c29ejqjxha0eow.jpg")
        binding.list = list1
        goodsAdapter = GoodsAdapter()
        binding.rvSv.adapter = goodsAdapter
        binding.rvSv.layoutManager = GridLayoutManager(this,2)

        binding.sv.setOnQueryTextListener(object:SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                if(query!=null){
                    //viewModel.searchNew(query,10,1)
                    viewModel.newSearch(query)
                    RecordDBUtils.getDb().getRecordDao().insert(Record(query))
                    onResume()
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return false
            }
        })
    }

    override fun initData() {
        viewModel.success.observe(this){
            if(it.code==200){
                goodsAdapter.data.clear()
                goodsAdapter.data.addAll(it.data.list)
                goodsAdapter.notifyDataSetChanged()
            }
        }
        viewModel.newSuccess.observe(this){
            goodsAdapter.data.clear()
            goodsAdapter.data.addAll(it)
            goodsAdapter.notifyDataSetChanged()
        }
    }

    override fun getViewModel(): Class<SearchNewViewModel> {
        return SearchNewViewModel::class.java
    }

    override fun getLayout(): Int {
        return  R.layout.activity_search_new
    }

    override fun onResume() {
        super.onResume()
        //查询数据库
        val list = RecordDBUtils.getDb().getRecordDao().query()
        if(list.size>0){
            //先把之前的清空
            binding.cg.removeAllViews()
            list.forEach {
                var key = it.keyword
                val chip = LayoutInflater.from(this).inflate(R.layout.item_chip_new, null) as Chip
                chip.findViewById<TextView>(R.id.name).text = it.keyword//找到视图中的控件进行赋值
                binding.cg.addView(chip)//显示在cg上
                //点击再次查询
                chip.setOnClickListener {
                    binding.sv.setQuery(key,true)
                }
                //点击关闭
                chip.setOnCloseIconClickListener {
                    binding.cg.removeView(it)
                    RecordDBUtils.getDb().getRecordDao().delete(Record(key))

                }
            }
        }
    }

}